/**
 * @Title: HomeActivity.java
 * @Package com.sinoglobal.waitingMe.activity
 * @Description: TODO(用一句话描述该文件做什么)
 * @author hpq
 * @date 2015-8-30 上午11:55:09
 */

package com.sinoglobal.wallet.activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sinoglobal.wallet.R;
import com.sinoglobal.wallet.activity.W_SinoBaseActivity;
import com.sinoglobal.wallet.api.RemoteImpl;
import com.sinoglobal.wallet.app.SinoConstans;
import com.sinoglobal.wallet.app.SinoValueManager;
import com.sinoglobal.wallet.entity.W_HomeBalanceEntity;
import com.sinoglobal.wallet.entity.W_HomeBalanceEntity.RsEntity;
import com.sinoglobal.wallet.receiver.W_ChangeBalanceReciver;
import com.sinoglobal.wallet.receiver.W_NetworkStatusReceiver;
import com.sinoglobal.wallet.task.MyAsyncTask;
import com.sinoglobal.wallet.util.LogUtils;
import com.sinoglobal.wallet.util.NetWorkUtil;

/**
 * @author clf 
 * @createdate 2015-10-22 
 * @Description: TODO(主页)
 */
public class W_HomeActivity extends W_SinoBaseActivity implements OnClickListener{
	private TextView notice_text,account_balance_text,my_bankcard_num,account_balance_right_red_dot;
	private RelativeLayout account_balance,recharge_layout,withdrawal_layout,my_bankcard_layout,pay_password_layout,coupons_layout;
	private String blanceTxt="0.0";
	private String isHaspass="";
//	private int bankCardNum;
	private BalanceHandler balanceHandler;
	private W_ChangeBalanceReciver ChangeBalanceReciver;
	private W_NetworkStatusReceiver netStatusReceiver;
//	private String BalanceChange;
	private String userId="";


	public class BalanceHandler extends Handler{
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what){
			case 1:
				LogUtils.v("(CharSequence) msg.obj---"+(CharSequence) msg.obj);
				blanceTxt=(String) msg.obj;
				account_balance_text.setText("￥"+(String) msg.obj);
				account_balance_right_red_dot.setVisibility(View.VISIBLE);	
				break;
			case 2:
				isHaspass="Y";
				notice_text.setVisibility(View.GONE);
				break;
			case 3:
				String str=(String) msg.obj;
				if(str.equals("0")){
					my_bankcard_num.setText("未添加");
				}else{
				my_bankcard_num.setText((String) msg.obj+"张");}
				break;

			}
		}}
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.wallet_activity_main);
		showView(mRightLayout);
		userId = SinoValueManager.getUserId(this);
		init();
		initReciver();
	/*	if(getIntent().getStringExtra("IsHasPass")!=null){
			blanceTxt = getIntent().getStringExtra("Balance");
			BalanceChange = getIntent().getStringExtra("BalanceChange");
			isHaspass = getIntent().getStringExtra("IsHasPass");
			bankCardNum = getIntent().getIntExtra("BankCardNum", 0);
		}else{
			
		}*/
		getData();
	//	initData();
		monitorNetworkStatus();
	}
	private void monitorNetworkStatus() {
		IntentFilter filter = new IntentFilter();
		filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		netStatusReceiver = new W_NetworkStatusReceiver();
		registerReceiver(netStatusReceiver, filter);
	}
	private void initReciver() {
		// TODO Auto-generated method stub
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(SinoConstans.KEY_BalanceReceiver);
		ChangeBalanceReciver = new W_ChangeBalanceReciver(balanceHandler);
		registerReceiver(ChangeBalanceReciver, intentFilter);	
	}

	/*private void initData() {
		// TODO Auto-generated method stub
		if(isHaspass.equals("N")){
			notice_text.setVisibility(View.VISIBLE);
		}else{
			notice_text.setVisibility(View.GONE);
		}
		account_balance_text.setText(blanceTxt+"元");
		if(bankCardNum==0){
			my_bankcard_num.setText("未添加");	
		}else{
			my_bankcard_num.setText(bankCardNum+"张");	
		}
		if("Y".equals(BalanceChange)){
			account_balance_right_red_dot.setVisibility(View.VISIBLE);	
		}else{
			account_balance_right_red_dot.setVisibility(View.GONE);	
		}
	}*/

	private void init(){
		balanceHandler = new BalanceHandler();
		notice_text = (TextView)findViewById(R.id.notice_text);
		account_balance_text = (TextView)findViewById(R.id.account_balance_text);
		my_bankcard_num= (TextView)findViewById(R.id.my_bankcard_num);
		account_balance_right_red_dot= (TextView)findViewById(R.id.account_balance_right_red_dot);
		account_balance = (RelativeLayout)findViewById(R.id.account_balance);
		recharge_layout = (RelativeLayout)findViewById(R.id.recharge_layout);
		withdrawal_layout = (RelativeLayout)findViewById(R.id.withdrawal_layout);
		my_bankcard_layout = (RelativeLayout)findViewById(R.id.my_bankcard_layout);
		pay_password_layout = (RelativeLayout)findViewById(R.id.pay_password_layout);
		coupons_layout = (RelativeLayout)findViewById(R.id.coupons_layout);

		notice_text.setOnClickListener(this);
		account_balance.setOnClickListener(this);
		recharge_layout.setOnClickListener(this);
		withdrawal_layout.setOnClickListener(this);
		my_bankcard_layout.setOnClickListener(this);
		pay_password_layout.setOnClickListener(this);
		coupons_layout.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		int id = v.getId();
		if (id == R.id.notice_text) {
			if ("".equals(userId)||userId==null) {
				goLogin();
			} else {
				Bundle setting_bundle = new Bundle();
				setting_bundle.putString("title_flag", "3");
				goIntent(W_PayPasswordActivity.class, setting_bundle);// 设置密码
			}
		} else if (id == R.id.account_balance) {
			if ("".equals(userId)||userId==null) {
				goLogin();
			} else {
				account_balance_right_red_dot.setVisibility(View.GONE);
				goIntent(W_BalanceDetailActivity.class);// 账号余额
			}
		} else if (id == R.id.recharge_layout) {// 充值
			if ("".equals(userId)||userId==null) {
				goLogin();
			} else {
				goIntent(W_RechargelActivity.class);
			}
		} else if (id == R.id.withdrawal_layout) {// 提现
			if ("".equals(userId)||userId==null) {
				goLogin();
			} else {
				Bundle setting_bundle = new Bundle();
				setting_bundle.putString("IsHasPass", isHaspass);
				setting_bundle
						.putDouble("BlanceTxt", Double.valueOf(blanceTxt));
				goIntent(W_WithDrawalActivity.class, setting_bundle);
			}
		} else if (id == R.id.my_bankcard_layout) {// 我的银行卡
			if ("".equals(userId)||userId==null) {
				goLogin();
			} else {
				goIntent(W_MyBankActivity.class);
			}
		} else if (id == R.id.pay_password_layout) {// 支付密码
			if ("".equals(userId)||userId==null) {
				goLogin();
			} else {
				if (isHaspass.equals("N")) {
					Bundle setting_bundle = new Bundle();
					setting_bundle.putString("title_flag", "3");
					goIntent(W_PayPasswordActivity.class, setting_bundle);// 设置密码
				} else if (isHaspass.equals("Y")) {
					goIntent(W_PayPasswordUpdateActivity.class);
				}
			}

		} else if (id == R.id.coupons_layout) {// 优惠券
			if ("".equals(userId)||userId==null) {
				goLogin();
			} else {
				goIntent(W_CouponActivity.class);
			}
		} else if (id == R.id.wallet_noData) {// 网络断开显示页面点击事件
			if (NetWorkUtil.getNetWorkInfoType(getApplicationContext()) == NetWorkUtil.NONE) {
				showToast("请检查网络是否连接！");
				loadError(true);
			} else {
				loadError(false);
				;
				getData();
			}
		}
	}
	/**
	 * 未登录情况下跳转登陆界面
	 */
public void goLogin(){
			goIntent(SinoConstans.APP_LoginAction); 
			finish();
}

	private void getData() {
		MyAsyncTask<W_HomeBalanceEntity> task = new MyAsyncTask<W_HomeBalanceEntity>(true,this,true) {
			@Override
			public W_HomeBalanceEntity before(Void... params) throws Exception {
				// TODO 测试接口
				return RemoteImpl.getInstance().getBalance(W_HomeActivity.this);
			}

			@Override
			public void after(W_HomeBalanceEntity result) {
				if (result != null) {
					if (result.getCode().equals("100")) {
						RsEntity rs=result.getRs();
						if(rs.getIsHasPass() != null){
							if(rs.getIsHasPass().equals("N")){
								notice_text.setVisibility(View.VISIBLE);
							}else{
								notice_text.setVisibility(View.GONE);
							}
						}
						
						blanceTxt = rs.getBalance();
						isHaspass = rs.getIsHasPass();
						account_balance_text.setText("￥"+rs.getBalance());
                     if(rs.getBalance()==null||userId.equals("")){
                    	 account_balance_text.setText("请登录");
						}
						if(rs.getBankCardNum()==0){
							my_bankcard_num.setText("未添加");	
						}else{
							my_bankcard_num.setText(rs.getBankCardNum()+"张");	
						}
						if("Y".equals(rs.getBalanceChange())){
							account_balance_right_red_dot.setVisibility(View.VISIBLE);	
						}else{
							account_balance_right_red_dot.setVisibility(View.GONE);	
						}
					}else{
						Toast.makeText(W_HomeActivity.this, "数据加载失败", 0).show();
					}
				}

			}

			@Override
			public void exception() {
			}
		};
		task.execute();
	}
	@Override
	protected void onNewIntent(Intent intent) {
		// TODO Auto-generated method stub
		super.onNewIntent(intent);
		getData();
		
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(ChangeBalanceReciver);
		unregisterReceiver(netStatusReceiver);//网络监控注销
		if (balanceHandler != null) {
			balanceHandler.removeCallbacksAndMessages(null); //当界面销毁时清除handler队列信息
		}
	}
}
